using System;
using System.Collections.Generic;
using System.Text;
using PerfilEmpreendedor.Bo.Model;
using PerfilEmpreendedor.Dao.Interfaces;
using NHibernate.Criterion;
using System.Collections;
using NHibernate;
using System.Linq;

namespace PerfilEmpreendedor.Dao.Implements
{
    public class CartelaDao:HibernateDao<Cartela>,ICartelaDao
    {
        public IList List()
        {
            return session.CreateCriteria(typeof(Cartela))
               .CreateAlias("Nivel", "niv")
               .CreateAlias("Tipo", "tp")
               .SetProjection(Projections.ProjectionList()
               .Add(Projections.Property("Id"))
               .Add(Projections.Property("Resposta"))
               .Add(Projections.Property("tp.Descricao"))
               .Add(Projections.Property("niv.Descricao")))
               .List();
        }
        public IList Pesquisar(String keyword)
        {
            return session.CreateCriteria(typeof(Cartela))
               .CreateAlias("Nivel", "niv")
               .CreateAlias("Tipo", "tp")
               .Add(Restrictions.Like("Resposta", "%" + keyword + "%"))
               .SetProjection(Projections.ProjectionList()
               .Add(Projections.Property("Id"))
               .Add(Projections.Property("Resposta"))
               .Add(Projections.Property("tp.Descricao"))
               .Add(Projections.Property("niv.Descricao")))
               .List();
        }


        public IList<Cartela> ListarParaJogo(Partida p,Int32 idTipo)
        {
            IList<Cartela> lstRetorno =session.QueryOver<Cartela>().Where(x => x.Nivel.Id == p.Nivel.Id && x.Tipo.Id == idTipo).List();
            IList<Cartela> lstCartelasUsadas=new List<Cartela>();

            foreach (var h in p.Historicos)
            {
                lstCartelasUsadas.Add(h.Cartela);
            }

            var query = (from cart in lstRetorno
                         where !(from hist in lstCartelasUsadas select hist).Contains(cart)
                         select cart);
            return query.ToList();
        }
        public IList<Cartela> ListarParaJogo(Partida p)
        {
            IList<Cartela> lstRetorno = session.QueryOver<Cartela>().Where(x => x.Nivel.Id == p.Nivel.Id).List();
            IList<Cartela> lstCartelasUsadas = new List<Cartela>();

            foreach (var h in p.Historicos)
            {
                lstCartelasUsadas.Add(h.Cartela);
            }

            var query = (from cart in lstRetorno
                         where !(from hist in lstCartelasUsadas select hist).Contains(cart)
                         select cart);
            return query.ToList();
        }
    }
}
